<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 /**
  * Oauth2 SocialAuth for CodeIgniter
  * QQ 授权模块
  * @author     Calvin <hongbinfu@163.com>
  */

class OAuth2_Provider_Weixin_mobile extends OAuth2_Provider
{
	public $name = 'weixin';
	public $human = '微信';
	public $uid_key = 'openid';
	public $client_id_key = 'appid';
	public $client_secret_key = 'secret';
	protected $scope = 'snsapi_userinfo';
	public $method = 'POST';

	public $is_mobile=FALSE;

	public function url_authorize()
	{
		return 'https://open.weixin.qq.com/connect/oauth2/authorize';
	}

	public function url_access_token()
	{
		return 'https://api.weixin.qq.com/sns/oauth2/access_token';
	}

	public function get_user_info(OAuth2_Token_Access $token)
	{
		$url = 'https://api.weixin.qq.com/sns/userinfo';
		$user = $this->ci->curl->simple_get($url,array(
				'access_token' => $token->access_token,
				'openid' => $token->uid,
				'lang' => 'zh_CN'
		));
		$user = json_decode($user);
		if (array_key_exists("errcode", $user)) {
			throw new OAuth2_Exception((array) $user);
		}
                
		return array(
            'from' => $this->from,
			'openid' => $user->openid,
			'name' => $user->nickname,
			'head_img' => $user->headimgurl,
			'access_token' => $token->access_token,
			'expires_date' => $token->expires,
			'refresh_token' => $token->refresh_token,
			'unionid'=>	$user->unionid
		);
	}
}
